package gipfProject.games.zertz.gui;

import gipfProject.tools.ResourceLoader;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.QuadCurve2D;

import javax.swing.JPanel;

import gipfProject.games.zertz.data.ZertzData;

public class DisplayMoveUI extends JPanel {
	ZertzData gameState;
	static Color backgroundColor;
	ResourceLoader images;

	DisplayMoveUI(ResourceLoader images, ZertzData gameState) {
		this.gameState = gameState;
		this.images = images;
		backgroundColor = Color.blue;
	}

	// TODO:
	public void paintComponent(Graphics g) {
		String textMove, textPlayer;

		// Set our font
		Font textFont = new Font("Helvetica", Font.BOLD, 14);
		g.setFont(textFont);

		// Clear the existing display
		g.setColor(backgroundColor);
		g.fillRect(0, 0, getSize().width, getSize().height);

		// Draw a border around this box
		g.setColor(Color.black);
		g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);

		// Set our text
		textPlayer = gameState.getCurrentPlayerName(); 
		textMove = gameState.getCurrentMoveName();
		
		// Set our colors
		Color textColor, backColor;
		if (gameState.isWhitePlayer()){
			textColor = Color.black;
			backColor = Color.white;
		}else{
			textColor = Color.white;
			backColor = Color.black;
		}
		
		// Create a box for the "current player" display
		g.drawLine(1, 23, getSize().width, 23);

		g.setColor(backColor);
		g.fillRect(1, 1, getSize().width - 2, 22);
	
		// Display the current player's name
		g.setColor(textColor);
		drawCenteredString(g, textPlayer, textFont, 15, getSize().width);

		// Display the current move
		g.setColor( Color.white );
		drawCenteredString(g, textMove, textFont, 90, getSize().width);

		// Set up our 2D graphics
		Graphics2D g2 = (Graphics2D) g;

		// Change the line-color to yellow
		g2.setColor(Color.yellow);

		// Change the line thickness to 3 pixels
		g2.setStroke(new BasicStroke(3));

		// Draw graphics for moveCapture
		if (gameState.moveType == gameState.captureMarble) {
			g.drawImage(images.getResource("ZertzRing"), 5, 40, this);
			g.drawImage(images.getResource("WhiteMarble"), 14, 49, this);

			g.drawImage(images.getResource("ZertzRing"), 45, 40, this);
			g.drawImage(images.getResource("BlackMarble"), 54, 49, this);

			g.drawImage(images.getResource("ZertzRing"), 85, 40, this);

			// Draw a curve indicating the first marble "jumping" the second
			QuadCurve2D q = new QuadCurve2D.Float();
			q.setCurve(30, 40, 45, 20, 85, 40);
			g2.draw(q);

			// Draw an arrow-head at the end of the curve
			g2.drawLine(85, 40, 80, 42);
			g2.drawLine(85, 40, 83, 35);

			// Draw graphics for movePlace
		} else if (gameState.moveType == gameState.placeMarble) {
			g.drawImage(images.getResource("ZertzRing"), 25, 35, this);

			g.drawImage(images.getResource("WhiteMarble"), 85, 44, this);

			g2.drawLine(60, 50, 80, 50);
			g2.drawLine(60, 50, 65, 45);
			g2.drawLine(60, 50, 65, 55);

			// Draw graphics for moveRing
		} else {
			g.drawImage(images.getResource("ZertzRing"), 45, 40, this);

			g2.drawLine(61, 55, 61, 30);
			g2.drawLine(61, 30, 66, 35);
			g2.drawLine(61, 30, 56, 35);
		}

	}// End of paint()

	public void drawCenteredString(Graphics g, String input, Font textFont,
			int y, int panelWidth) {
		FontMetrics fm = g.getFontMetrics(textFont);
		java.awt.geom.Rectangle2D rect = fm.getStringBounds(input, g);

		// Center text horizontally
		int textWidth = (int) (rect.getWidth());
		int x = (panelWidth - textWidth) / 2;

		// Draw the string.
		g.drawString(input, x, y);

	}

}
